// pages/course/course.js
import { reqCourseAll } from '../../api/courseIndex';
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		bannerData: [],
		listData: [],
		channelId: 409,
		pageNo: 1,
		pageSize: 10,
		loading: 0
	},
	onLoad(options) {
		this.getData()
	},
	onReachBottom() {
		let page = this.data.pageNo + 1;
		if (this.data.loading == 2) {
			return;
		}
		this.setData({ pageNo: page, loading: 1 })
		this.getData();
	},
	async getData() {
		let { channelId, pageNo, pageSize } = this.data;
		let res = await reqCourseAll({ channelId, pageNo, pageSize });
		if (res.code == 1) {
			if (res.data.list.length == 0) {
				this.setData({ loading: 2 });
			} else {
				this.setData({ loading: 0 });
			}
			this.setData({ listData: [...this.data.listData, ...res.data.list] });
			this.setData({ bannerData: this.data.listData.slice(0, 5) });
		}
	},
	toSearch() {
		wx.navigateTo({ url: '/pages/courseSearch/courseSearch' })
	},
	toDetailPage(e) {
		wx.navigateTo({ url: e.currentTarget.dataset.url })
	},
	toCourseDetail(e) {
		let id = e.currentTarget.dataset.id;
		wx.navigateTo({ url: `/pages/courseTeachRoom/courseTeachRoom?id=${id}` })
	}
})